package karileet.meeting.profile;

import android.content.Context;
import android.media.AudioManager;

/**
 * Class for enabling/disabling app
 * 
 * @author markus karileet
 */
public class MeetingProfile {
	private Context ctx;

	public MeetingProfile(Context ctx) {
		this.ctx = ctx;
	}

	/**
	 * Method enables app and stores user current configuration
	 * @param ProfileState state App state/configuration
	 */
	public void enable(ProfileState state) {
		ProfileNotifications notification = new ProfileNotifications(ctx);
		UserConf uc = new UserConf(ctx);
		uc.save();
		if (state.isVibrateEnabled()) {
			uc.set(AudioManager.RINGER_MODE_VIBRATE);
		} else {
			uc.set(AudioManager.RINGER_MODE_SILENT);
		}
		notification.initiateNotification();
		state.setProfileEnabled(true);
	}

	/**
	 * Method disables app, restoring user previous configuration
	 * @param ProfileState state App state/configuration
	 */
	public void disable(ProfileState state) {
		ProfileNotifications notification = new ProfileNotifications(ctx);
		UserConf uc = new UserConf(ctx);
		int mode = uc.read();
		uc.set(mode);
		notification.cancelNotification();
		state.setProfileEnabled(false);
	}
	
	/**
	 * Method updates profile state (enable/disable vibrator) when
	 * profile is "on"
	 * @param state
	 */
	public void update(ProfileState state) {
		if (state.isProfileEnabled()) {
			UserConf uc = new UserConf(ctx);
			if (state.isVibrateEnabled()) {
				uc.set(AudioManager.RINGER_MODE_VIBRATE);
			} else {
				uc.set(AudioManager.RINGER_MODE_SILENT);
			}
		}
	}
}
